package com.source.lightfowl.person.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.source.lightfowl.authorization.CheckPermissions;
import com.source.lightfowl.person.dao.BigPlateDao;
import com.source.lightfowl.person.dao.PlateDao;
import com.source.lightfowl.person.service.*;
import com.source.lightfowl.person.entity.*;
import com.source.lightfowl.syslog.annotation.LogAnnotation;
import com.source.lightfowl.utils.MyResult;
import com.source.lightfowl.utils.UUIDUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

@RestController
@CrossOrigin
@Api(tags = "角色管理")
public class RoleController {
    @Autowired
    private RoleService roleService;
    @Autowired
    private RolePlateService rolePlateService;
    @Autowired
    private PlateService plateService;
    @Autowired
    private UrlService urlService;

    //创建角色+角色权限(测试通过)
    @ApiOperation(value = "创建角色+角色权限")
    @PostMapping("/addRole")
    @Transactional
    @CheckPermissions(value = "l9:s2:t1")
    public MyResult AddRole(String rname,@RequestParam List<String> arr2/*@RequestBody String arr2*/){
        //List<List<String>> plate_element = JSON.parseObject(arr2, new TypeReference<List<List<String>>>() {}); // Json 转List
        Role role = new Role();
        String rid = UUIDUtils.getUUID();
        //封装role
        role.setRid(rid);
        role.setRname(rname);
        roleService.addRole(role);

        try {
            for (String a : arr2){
                Role_Plate rolePlate = new Role_Plate();
                Role_Url role_url = new Role_Url();
                //截取两字符
                String bpid = a.substring(0, 2);
                String ptype = a.substring(2, 4);
                String eid = a.substring(4, 6);
                //封装
                rolePlate.setRid(rid);
                rolePlate.setBpid(bpid);
                rolePlate.setPid(plateService.querryPlate(ptype, bpid).getPid());
                rolePlate.setPtype(ptype);
                rolePlate.setEids(eid);
                Url url = urlService.findUrl(ptype, bpid);
                role_url.setRid(rid);
                role_url.setUid(url.getId());

                rolePlateService.addRoleplate(rolePlate);
                urlService.saveRoleUrl(role_url);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return MyResult.build(1, "失败");
        }
        return MyResult.build(200, "成功");


        /*
            for (List<String> pe : plate_element) {
                Role_Plate rolePlate = new Role_Plate();
                Role_Url role_url = new Role_Url();
                for (int i = 0; i < pe.size(); i++) {
                    //封装rolePlate
                    rolePlate.setRid(rid);
                    rolePlate.setBpid(pe.get(0));
                    rolePlate.setPid(pe.get(1));
                    rolePlate.setEids(pe.get(2));
                    Url url = urlService.findUrl(pe.get(1));
                    role_url.setRid(rid);
                    role_url.setUid(url.getId());
                }
                rolePlateService.addRoleplate(rolePlate);
                urlService.saveRoleUrl(role_url);
            }*/
    }



    //删除角色（测试通过）
    @ApiOperation(value = "删除角色")
    @DeleteMapping("/deleteRoleByRid")
    @CheckPermissions(value = "l9:s2:t2")
    public MyResult deleteRole(@RequestParam("rid") String rid){
        try {
            roleService.deleteRole(rid);
            rolePlateService.deleteRolePlate(rid);
            urlService.deleteRoleUrl(rid);
        } catch (Exception e) {
            e.printStackTrace();
            return MyResult.build(1, "失败");
        }
        return MyResult.build(200, "成功");
    }

    //修改角色
    @ApiOperation(value = "修改角色")
    @PostMapping("/updateRole")
    @CheckPermissions(value = "l9:s2:t3")
    public MyResult updateRole(@RequestParam String id, @RequestParam List<String> arr2, String rolename){

        Role role = new Role();
        role.setRid(id);
        role.setRname(rolename);
        try {
            roleService.updateRole(role);
            //先删掉原来计入的role——plate
            rolePlateService.deleteRolePlate(id);
            //先删掉原来计入的url
            urlService.deleteRoleUrl(id);
            //List<Role_Plate> role_plates = rolePlateService.querryRolePlateByRid(id);
            for (String a : arr2){
                Role_Plate rolePlate = new Role_Plate();
                Role_Url role_url = new Role_Url();
                //截取两字符
                String bpid = a.substring(0, 2);
                String ptype = a.substring(2, 4);
                String eid = a.substring(4, 6);
                //封装
                rolePlate.setRid(id);
                rolePlate.setBpid(bpid);

                try {
                    rolePlate.setPid(plateService.querryPlate(ptype, bpid).getPid());
                } catch (Exception e) {
                    e.printStackTrace();
                    return MyResult.build(1, "失败,无此类子板块");
                }
                rolePlate.setPtype(ptype);
                rolePlate.setEids(eid);
                Url url = urlService.findUrl(ptype, bpid);
                role_url.setRid(id);
                role_url.setUid(url.getId());

                rolePlateService.addRoleplate(rolePlate);
                urlService.saveRoleUrl(role_url);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return MyResult.build(1, "失败");
        }
        return MyResult.build(200, "成功");


    }


    @ApiOperation(value = "查询所有角色")
    @PostMapping("/queryRoleAll")
    @CheckPermissions(value = "l9:s2:t4")
    public MyResult querryRoleAll() {
        List<Role> roleList = roleService.querryRoleAll();
        List<RoleAll> roleAllList = new LinkedList<>();
        try {
            for (int i = 0; i < roleList.size(); i++) {
                RoleAll roleAll = new RoleAll();
                List<String> auth_list = new ArrayList<>();
                List<Role_Plate> role_plateList = rolePlateService.querryRolePlateByRid(roleList.get(i).getRid());

                System.out.println("角色——板块的size =======" + role_plateList.size());
                try {
                    for (int j = 0; j < role_plateList.size(); j++) {
                        //封装RoleAll

                        roleAll.setRid(roleList.get(i).getRid());
                        roleAll.setRname(roleList.get(i).getRname());
                        String s = new String();

                        Plate plate = plateService.querryPlateByPid(role_plateList.get(j).getPid());

                        Big_Plate big_plate = plateService.querryBigPlate(plate.getBigpid());

                        s = big_plate.getBpid() + plate.getPtype() + role_plateList.get(j).getEids();
                        auth_list.add(s);
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                    return MyResult.build(1, "权限查询失败");
                }
                roleAll.setAuth_list(auth_list);
                roleAllList.add(roleAll);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return MyResult.build(1, "失败");
        }
        return MyResult.build(200, "成功", roleAllList);
    }

    @ApiOperation(value = "id条件查询角色")
    @PostMapping("/queryRoleById")
    @CheckPermissions(value = "l9:s2:t4")
    public MyResult querryRoleById(@RequestParam String id){
        Role role = roleService.querryUserPower(id);
        List<RoleAll> roleAllList = new LinkedList<>();

                RoleAll roleAll = new RoleAll();
                List<String> auth_list = new ArrayList<>();
                List<Role_Plate> role_plateList = rolePlateService.querryRolePlateByRid(id);
                try {
                    for (int j = 0; j < role_plateList.size(); j++) {
                        //封装RoleAll

                        roleAll.setRid(role.getRid());
                        roleAll.setRname(role.getRname());
                        String s = new String();

                        Plate plate = plateService.querryPlateByPid(role_plateList.get(j).getPid());

                        Big_Plate big_plate = plateService.querryBigPlate(plate.getBigpid());

                        s = big_plate.getBpid() + plate.getPtype() + role_plateList.get(j).getEids();
                        auth_list.add(s);
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                    return MyResult.build(1, "权限查询失败");
                }
                roleAll.setAuth_list(auth_list);
                roleAllList.add(roleAll);

        return MyResult.build(200, "成功", roleAllList);
    }

    //条件查询角色
    @ApiOperation(value = "条件查询角色")
    @PostMapping("/queryRoleByRname")
    @CheckPermissions(value = "l9:s2:t4")
    public MyResult querryRoleByRname(@RequestParam("rname") String rname){
        List<Role> roleList = roleService.querryRole(rname);
        List<RoleAll> roleAllList = new LinkedList<>();
        try {
            for (int i = 0; i < roleList.size(); i++) {
                RoleAll roleAll = new RoleAll();
                List<String> auth_list = new ArrayList<>();
                List<Role_Plate> role_plateList = rolePlateService.querryRolePlateByRid(roleList.get(i).getRid());
                try {
                    for (int j = 0; j < role_plateList.size(); j++) {
                        //封装RoleAll

                        roleAll.setRid(roleList.get(i).getRid());
                        roleAll.setRname(roleList.get(i).getRname());
                        String s = new String();

                        Plate plate = plateService.querryPlateByPid(role_plateList.get(j).getPid());

                        Big_Plate big_plate = plateService.querryBigPlate(plate.getBigpid());

                        s = big_plate.getBpid() + plate.getPtype() + role_plateList.get(j).getEids();
                        auth_list.add(s);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    return MyResult.build(1, "权限查询失败");
                }
                roleAll.setAuth_list(auth_list);
                roleAllList.add(roleAll);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return MyResult.build(200, "成功", roleAllList);
    }


    @Autowired
    private BigPlateDao bigPlateDao;

    @Autowired
    private PlateDao plateDao;

    @PostMapping("/addplate")
    public String addplate(String pname,String ptype, String bpid){
        String pid = UUIDUtils.getUUID();
        Plate plate = new Plate();
        plate.setBigpid(bpid);
        plate.setPid(pid);
        plate.setPname(pname);
        plate.setPtype(ptype);
        plateDao.save(plate);
        return "success";
    }

    @PostMapping("/addBplate")
    public String addBplate(String bpname, String bpid) {
        Big_Plate bigPlate = new Big_Plate();
        bigPlate.setBpid(bpid);
        bigPlate.setBpname(bpname);
        bigPlateDao.save(bigPlate);
        return "success";
    }


     /*
    //条件查询角色（测试通过）
    @ApiOperation(value = "条件查询角色")
    @PostMapping("/queryRoleByRname")
    @ResponseBody
    public List<RoleAll> querryRoleByRname(@RequestParam("rname") String rname){
        List<Role> roleList = roleService.querryRole(rname);
        List<RoleAll> roleAllList = new LinkedList<>();
        try {
            for (int i = 0; i < roleList.size(); i++) {
                List<Role_Plate> role_plateList = rolePlateService.querryRolePlateByRid(roleList.get(i).getRid());
                try {
                    for (int j = 0; j < role_plateList.size(); j++) {
                        //封装RoleAll
                        RoleAll roleAll = new RoleAll();
                        roleAll.setRid(roleList.get(i).getRid());
                        roleAll.setRname(roleList.get(i).getRname());
                        roleAll.setEids(role_plateList.get(j).getEids());
                        Plate plate = plateService.querryPlateByPid(role_plateList.get(j).getPid());
                        roleAll.setPname(plate.getPname());
                        Big_Plate big_plate = plateService.querryBigPlate(role_plateList.get(j).getBpid());
                        roleAll.setBpname(big_plate.getBpname());
                        roleAllList.add(roleAll);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
     return roleAllList;
    }

     */

    /*
    //查询所有角色（通过测试）
    @ApiOperation(value = "查询所有角色")
    @PostMapping("/queryRoleAll")
    @ResponseBody
    public List<RoleAll> querryRoleAll() {
        List<Role> roleList = roleService.querryRoleAll();
        List<RoleAll> roleAllList = new LinkedList<>();
        try {
            for (int i = 0; i < roleList.size(); i++) {
                List<Role_Plate> role_plateList = rolePlateService.querryRolePlateByRid(roleList.get(i).getRid());
                try {
                    for (int j = 0; j < role_plateList.size(); j++) {
                        //封装RoleAll
                        RoleAll roleAll = new RoleAll();
                        roleAll.setRid(roleList.get(i).getRid());
                        roleAll.setRname(roleList.get(i).getRname());
                        roleAll.setEids(role_plateList.get(j).getEids());
                        Plate plate = plateService.querryPlateByPid(role_plateList.get(j).getPid());
                        roleAll.setPname(plate.getPname());
                        Big_Plate big_plate = plateService.querryBigPlate(plate.getBigpid());
                        roleAll.setBpname(big_plate.getBpname());
                        roleAllList.add(roleAll);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return roleAllList;
    }

     */
}

